package com.bci.pwtz.controller.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.admin.service.AdminCompanyService;
import com.bci.pwtz.admin.service.AdminExcelUtilService;
import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.model.Company;
import com.bci.pwtz.mysql.model.UserCompanyRef;
import com.bci.pwtz.service.UserCompanyRefService;

@Controller
@RequestMapping("/admin/excel")
public class AdminExcelUtilController  extends AdminAbstractController{
	@Autowired
    private AdminExcelUtilService excelUtilService;
	
	@Autowired
    private AdminCompanyService adminCompanyService;
	
	@Autowired
	private UserCompanyRefService userCompanyRefService;
	@RequestMapping(value="/companystatistics",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String>  companystatistics(String provinceCode, String cityCode, String beginTime, String endTime){
        List<Company> list = new ArrayList<Company>();
        Map<String, String> map=new HashMap<String, String>();
        String ret = "2";
        try
        {
        	String path = "";
        	if(PwtzConstants.THE_SERVER_TYPE==1){
        	    path=request.getSession().getServletContext().getRealPath("/")+"\\upload\\";
        	}else{
        	    path=request.getSession().getServletContext().getRealPath("/")+"/upload/";
        	    path = path.replaceAll("\\\\", "/");    //本地
        	}
//            path = path.replaceAll("/", "\\\\");    //服务器
            list= adminCompanyService.selectcCompanyStatistics(provinceCode, cityCode, beginTime, endTime);
            if(list!=null&&list.size()>0){
                ret = excelUtilService.exportCompanyListStatistics(list, path);
                if("2".equals(ret)){
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");    
                }else{
                    ret = ret.substring(ret.indexOf("upload"),ret.length());
                    ret = ret.replace("\\\\", "/");
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,ret);
                }
            }else{
                map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"没有可以导出的数据");
            }
        }
        catch (PwtzDatabaseException e)
        {
            e.printStackTrace();
        }
        return map;
    }
	
	@RequestMapping(value="/companylist",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String>  companylist(Company bean,String beginTime,String endTime){
        List<Company> list = new ArrayList<Company>();
        Map<String, String> map=new HashMap<String, String>();
        String ret = "2";
        try
        {
            String path = "";
            if(PwtzConstants.THE_SERVER_TYPE==1){
                path=request.getSession().getServletContext().getRealPath("/")+"\\upload\\";
            }else{
                path=request.getSession().getServletContext().getRealPath("/")+"/upload/";
                path = path.replaceAll("\\\\", "/");    //本地
            }
//            path = path.replaceAll("/", "\\\\");    //服务器
            list= adminCompanyService.findCompany(bean, beginTime, endTime);
            if(list!=null&&list.size()>0){
                ret = excelUtilService.exportCompanyStatistics(list, path);
                if("2".equals(ret)){
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");    
                }else{
                    ret = ret.substring(ret.indexOf("upload"),ret.length());
                    ret = ret.replace("\\\\", "/");
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,ret);
                }
            }else{
                map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"没有可以导出的数据");
            }
        }
        catch (PwtzDatabaseException e)
        {
            e.printStackTrace();
        }
        return map;
    }
	
	@RequestMapping(value="/userstatistics",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String>  userstatics(String beginTime, String endTime,String companyName){
        List<UserCompanyRef> list = new ArrayList<UserCompanyRef>();
        Map<String, String> map=new HashMap<String, String>();
        String ret = "2";
        try
        {
            String path = "";
            if(PwtzConstants.THE_SERVER_TYPE==1){
                path=request.getSession().getServletContext().getRealPath("/")+"\\upload\\";
            }else{
                path=request.getSession().getServletContext().getRealPath("/")+"/upload/";
                path = path.replaceAll("\\\\", "/");    //本地
            }
//            path = path.replaceAll("/", "\\\\");    //服务器
            list= userCompanyRefService.userStatics(beginTime, endTime, companyName);
            if(list!=null&&list.size()>0){
                ret = excelUtilService.exportUserstatics(list, path);
                if("2".equals(ret)){
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");    
                }else{
                    ret = ret.substring(ret.indexOf("upload"),ret.length());
                    ret = ret.replace("\\\\", "/");
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,ret);
                }
            }else{
                map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"没有可以导出的数据");
            }
        }
        catch (PwtzDatabaseException e)
        {
            e.printStackTrace();
        }
        return map;
    }
	
	@RequestMapping(value="/userlist",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String>  userlist(Long companyId, int pageSize,int pageNo){
        List<UserCompanyRef> list = new ArrayList<UserCompanyRef>();
        PaginationList<UserCompanyRef> plist = new PaginationList<UserCompanyRef>();
        Map<String, String> map=new HashMap<String, String>();
        String ret = "";
        try
        {
            String path = "";
            if(PwtzConstants.THE_SERVER_TYPE==1){
                path=request.getSession().getServletContext().getRealPath("/")+"\\upload\\";
            }else{
                path=request.getSession().getServletContext().getRealPath("/")+"/upload/";
                path = path.replaceAll("\\\\", "/");    //本地
            }
//            path = path.replaceAll("/", "\\\\");    //服务器
            plist= userCompanyRefService.selectCompanyUserList(companyId, pageSize, pageNo);
            if(plist!=null&&plist.getRecords()!=null&&plist.getRecords().size()>0){
                list=plist.getRecords();
                ret = excelUtilService.exportCompanyUserList(list, path);
                if("2".equals(ret)){
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");    
                }else{
                    ret = ret.substring(ret.indexOf("upload"),ret.length());
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,ret);
                }
            }else{
                map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"没有可以导出的数据");
            }
        }
        catch (PwtzDatabaseException e)
        {
            e.printStackTrace();
        }
        return map;
    }
}
